package org.openrbac.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.openrbac.rbac.constant.CommonConstant;
import org.openrbac.rbac.entity.Auth;
import org.openrbac.rbac.entity.Role;
import org.openrbac.rbac.entity.User;
import org.openrbac.rbac.mapper.UserMapper;
import org.openrbac.rbac.service.UserService;
import org.openrbac.rbac.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Objects;


/**
 * @author bapie
 * @description 针对表【user(系统用户)】的数据库操作Service实现
 * @createDate 2024-04-03 15:37:15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public User getUser(String username) {
        if (Objects.isNull(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        return getOne(new QueryWrapper<User>().eq(CommonConstant.USERNAME, username).or(wrapper -> wrapper.eq("email", username)));
    }

    @Override
    public User getUser(String username, String token) {

        String name = username.replace("\"", "");
        System.out.println("name: " + name);
        User user = this.baseMapper.getUser(name);
        System.out.println("hi: " + user);
        return this.baseMapper.getUser(name);
    }

    @Override
    public List<Role> findRoles(String username) {
        if (Objects.isNull(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        Long userId = baseMapper.selectIdByUsername(username);
        if (Objects.isNull(userId)) {
            throw new RuntimeException("用户名不存在");
        }
        return baseMapper.listRolesByUserId(userId);
    }

    @Override
    public List<Auth> findAuths(String username) {
        if (Objects.isNull(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        Long userId = baseMapper.selectIdByUsername(username);
        if (Objects.isNull(userId)) {
            throw new RuntimeException("用户名不存在");
        }
        List<Long> roleIds = baseMapper.listRoleIdsByUserId(userId);
        if (Objects.isNull(roleIds)) {
            throw new RuntimeException("当前用户没有角色");
        }
        return baseMapper.listAuthByRoleIds(roleIds);
    }

    @Override
    public boolean editRoles(String username, List<Long> roleIds) {
        if (Objects.isNull(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (Objects.isNull(roleIds) || roleIds.isEmpty()) {
            throw new RuntimeException("没有需要修改的角色");
        }
        Long userId = baseMapper.selectIdByUsername(username);
        if (Objects.isNull(userId)) {
            throw new RuntimeException("用户名不存在");
        }
        baseMapper.deleteRolesByUserId(userId);
        return baseMapper.insertRolesBatch(userId, roleIds);
    }

    @Override
    public BufferedImage getCaptcha() {
        String code = defaultKaptcha.createText();

        if (code == null || redisUtil.hasHashKey(CommonConstant.CAPTCHA, code)) {
            throw new RuntimeException("验证码生成失败");
        }
        redisUtil.hashSet(CommonConstant.CAPTCHA, code, code,60 * 5);
        BufferedImage image = defaultKaptcha.createImage(code);
        return image;
    }

    @Override
    public Boolean checkCaptcha(String captcha) {
        String code = (String) redisUtil.hashGet(CommonConstant.CAPTCHA, captcha);
        if (code != null && code.equals(captcha)) {
            redisUtil.hashDelete(CommonConstant.CAPTCHA, captcha);
            return true;
        }
        return false;
    }

}




